package com.fzu.express.controller;

import com.fzu.express.annotation.SystemLog;
import com.fzu.express.entity.Type;
import com.fzu.express.service.TypeService;
import com.fzu.express.utils.PageVO;
import com.fzu.express.utils.R;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fzu.express.enums.EnableStatusEnum;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tz
 * @since 2022-11-3
 */
@RestController
@RequestMapping("/type")
public class TypeController {
    @Resource
    private TypeService typeService;

    /**
     * 分页条件查询
     * @param type 参数
     * @param pageNum 页码
     * @param pageSize 记录数
     * @return
     */
    @GetMapping
    public R list(Type type, @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        PageVO pageVO = typeService.listType(type, pageNum, pageSize);
        return R.ok().data(pageVO);
    }

    /**
     * 选框接口
     * @return
     */
    @GetMapping("/options")
    public R options() {
        LambdaQueryWrapper<Type> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Type::getStatus, EnableStatusEnum.ENABLE.getCode());
        List<Type> list = typeService.list(lambdaQueryWrapper);
        return R.ok().data(list);
    }

    /**
     * 新增
     * @param type 参数
     * @return R
     */
    @PostMapping
    @SystemLog(businessName = "新增快递类型")
    public R add(@RequestBody Type type) {
        typeService.addType(type);
        return R.ok();
    }

    /**
     * 修改
     * @param type 参数
     * @return
     */
    @PutMapping
    @SystemLog(businessName = "修改快递类型")
    public R update(@RequestBody Type type) {
        typeService.updateType(type);
        return R.ok();
    }

    /**
     * 删除
     * @param id 地址id
     * @return
     */
    @DeleteMapping("/{id}")
    @SystemLog(businessName = "删除快递类型")
    public R delete(@PathVariable("id") Long id) {
        typeService.deleteType(id);
        return R.ok();
    }
}

